<?php

namespace catchAdmin\phone\controller;

use catchAdmin\hunqing\model\Userinfos as UserinfosModel;
use catcher\base\CatchController;
use catcher\CatchAuth;
use catcher\CatchResponse;
use think\facade\Db;
use think\facade\Request;
use think\Image;

class login extends CatchController
{
    protected $userinfosModel;

    public function __construct(UserinfosModel $userinfosModel)
    {
        $this->userinfosModel = $userinfosModel;
    }

    public function image()
    {
        $file = request()->file('file');
        // 上传到本地服务器
        $savename = \think\facade\Filesystem::putFile( 'topic', $file);



        //topic/20230822/646f8431e2d88cbaf748783110c0a308.jp

        $images = Image::open(app()->getRootPath() ."public/images/".$savename);
        $images->thumb(1000,1000)->save(app()->getRootPath() ."public/images/".$savename);
//
//        $images = array_values(Request::file());
//        if (!count($images)) {
//            throw new FailedException('请选择图片上传');
//        }
//        $img = $upload->checkImages($file)->multiUpload(
//            count($file) < 2 ? $images[0] : $images
//        );
        return CatchResponse::success(Request::instance()->domain().'/images/'.$savename);
    }


    public function addinfo(CatchAuth $auth)
    {
        $data = Request::post();
        if (!empty($data['photo1'][0])){
            $data['image'] = $data['photo1'][0];

        }
        if (!empty($data['photo1'][1])){
            $data['image1'] = $data['photo1'][1];

        }
        if (!empty($data['photo1'][2])){
            $data['image2'] = $data['photo1'][2];

        }
        if (!empty($data['photo1'][3])){
            $data['image3'] = $data['photo1'][3];

        }
        if (!empty($data['photo1'][4])){
            $data['image4'] = $data['photo1'][4];

        }

        p($auth);

        unset($data['photo1']);
        unset($data['photo2']);
        unset($data['photo3']);
        unset($data['photo4']);
        unset($data['photo5']);

        $data['created_at'] = date('Y-m-d H:i:s');
        $data['user'] =$auth->user()->username;

        Db::name('userinfos')->insert($data);
        return CatchResponse::success();

    }


    public function addinf1o(CatchAuth $auth)
    {
        $data['info'] = request()->post('field101');
        $data['uid'] = request()->post('uid');
        $data['created_at'] = date('Y-m-d H:i:s');
        $data['user'] =$auth->user()->username;

        Db::name('userinfos')->where('id', $data['uid'])->update(['updated_at' => time()]);

        Db::name('info')->insert($data);
        return CatchResponse::success();
    }



    public function delbyid($id)
    {
//        $id = request()->post('id');
//        Db::name('userinfos')->where('id', $id)->update(['deleted_at'=>time()]);
//        return CatchResponse::success();
        return CatchResponse::success($this->userinfosModel->deleteBy($id));
    }

    public function getinfo($id)
    {
        return  CatchResponse::success($this->userinfosModel->where('id',$id)->find());
    }
}



;